<?php

namespace App\Http\Controllers\Commission;

use App\Helpers\AgentCommissionTrait;
use App\Helpers\CityCommisionTrait;
use App\Helpers\CommisionTrait;
use App\Helpers\DepartmentCommisionTrait;
use App\Http\Controllers\Controller;
use App\Models\Divide\Category;
use App\Models\Divide\PdLevel;
use Illuminate\Http\Request;

class CommissionController extends Controller
{
    use CommisionTrait,DepartmentCommisionTrait,CityCommisionTrait,AgentCommissionTrait;
    //

    public function setCommisson(Request $request){
        // 获取订单
        $orderId = $request->order_id;
        $pdOrders = $this->getOrder($orderId);
        // 获取品类下面的各大板块
        $category = Category::query()->where('category_id',$pdOrders->account_mode)->value('category_types');
        $money = $pdOrders->divide_money;
        if($category){
            foreach ($category as $item){
                $ratio = $item['ratio']/100;
                $divide_money = bcmul($money, $ratio, 2);
                switch ($item['id']){
                    case 1: // 合伙制
                        $this->partnerShip($pdOrders,$divide_money);
                    break;
                    case 3: // 区域代理制
                        $this->getCitys($pdOrders,$divide_money);
                    break;
                    case 4: // 代理商
                        $user = $this->getUser($pdOrders->user_id);
                        info('获取代理商User'.json_encode($user));
                        if($user){
                        $lastMoney = $this->UserCommission($divide_money,$pdOrders,$user,1);
                        if($user->pid&&$user->pid!=$user->gid&&$lastMoney>0){
                            $pUser = $this->getUser($user->pid);
                            if($pUser){
                               $lastMoney =  $this->UserCommission($lastMoney,$pdOrders,$pUser,2);
                               if($user->pid&&$user->pid!=$user->gid&&$lastMoney>0){ // 不是平台id
                                   $PpUser = $this->getUser($user->pid);
                                   $this->UserCommission($divide_money,$PpUser,$PpUser,3);
                               }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    /**
     * @param PdLevel $model
     * @param Request $request
     * @return mixed|void
     * 批量更新更新等级
     */
    public function setBatchUploadLevel(PdLevel $model,Request $request){
        if($request->filled('data')){
            if(is_array($request->data)){
                $data = $request->data;
            }else{
                $data = json_decode($request->data,true);
            }
            $array = [];
            foreach ($data as $key=>$item){
                $array[$key]['id'] = $item['id'];
                $array[$key]['award'] = $item['award'];
                $array[$key]['first_ratio'] = $item['first_ratio'];
                $array[$key]['first_ratio1'] = $item['first_ratio1'];
                $array[$key]['first_ratio3'] = $item['first_ratio3'];
                $array[$key]['inte_scale'] = $item['inte_scale'];
                $array[$key]['inte_scale_buy'] = $item['inte_scale_buy'];
                $array[$key]['is_borker'] = $item['is_borker'];
                $array[$key]['is_inte'] = $item['is_inte'];
                $array[$key]['is_inte_buy'] = $item['is_inte_buy'];
                $array[$key]['is_sale'] = $item['is_sale'];
                $array[$key]['level'] = $item['level'];
                $array[$key]['mode'] = $item['mode'];
                $array[$key]['name'] = $item['name'];
                $array[$key]['second_ratio'] = $item['second_ratio'];
                $array[$key]['second_ratio1'] = $item['second_ratio1'];
                $array[$key]['threshold'] = $item['threshold'];
            }
            $res = $model->query()
                ->upsert($array,['id'],['award','first_ratio','first_ratio1',
                    'first_ratio3','inte_scale','inte_scale_buy','is_borker','is_inte',
                    'is_inte_buy','is_sale','level','mode','name','second_ratio',
                    'second_ratio1','threshold']);
            return $this->success($res);
        }
    }
}
